<?php
require_once('../../libraryfiles/config.php');


$action = isset($_GET['action']) ? $_GET['action'] : '';

switch($action)
{

	case 'add' :
		addPages();
		break;

	case 'deleteImage':
		deleteImage();
		break;

	case 'muliDelete' :
		muliDelete();
		break;

	case 'publish' :
		publishSelected();
		break;


	default :
		header('Location: index.php');
		exit();
}
/*
 Function Publish/Unpublish selected id
 */
function publishSelected(){
	if(isset($_POST['id'])){
		if(isset($_GET['publish']) && (int)$_GET['publish']>=0 && (int)$_GET['publish']<=1){
			$status = (int)$_GET['publish'];
		}
		else{
			header('Location: index.php');
			exit();
		}
		//Statement to get selected id
		$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];

		$sql_pub = "UPDATE tblpages SET status = $status WHERE id IN ($id)";
		mysql_query($sql_pub);
	}

	header('Location: index.php?'.getAllGetParams(array('view','action','publish')));
	exit();
}

function addPages(){
	$detectExist_q = '';
	$ID = $_POST['hidid'];
	$link = '';
	$pgname = trim($_POST['txtpgname']);
	$description = mysql_real_escape_string($_POST['texteditor']);
	if(empty($pgname)){
		header("Location:".base_url_admin."/pages/index.php?view=add&error=required");
		exit();
	}
	
	if(!empty($ID)){
		$modify = true;
		$link = "&id=" . $ID;
		 $detectExist_q .= " AND id != " . $ID;
	}
	if(existRecord("tblpages","name = '$pgname' $detectExist_q")){
		$_SESSION['desc'] = @$description;
		header('Location:'.base_url_admin.'/pages/index.php?view=add' . $link . '&error=nameexist');
		exit();
	}else if(isset($modify)){
		$sql_u = "UPDATE tblpages SET name='$pgname', description='$description', modify_date=now() WHERE id = $ID";
		mysql_query($sql_u);
		header('Location:'.base_url_admin.'/pages/index.php?view=list&message=update');

	}else{
		if (isset($_SESSION['desc'])) {
			unset($_SESSION['desc']);
		}
		
		 $sql = "INSERT INTO tblpages(name, description, status, create_date, modify_date)
							VALUES('".$pgname."', '".$description."', 1, now(), now())";
		$result = mysql_query($sql);
		header('Location:'.base_url_admin.'/pages/index.php?view=list&message=add');
	}

}

function muliDelete()
{
	if(isset($_POST['id'])){

		//Statement to get selected id
		$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];

		//Statment to delete selected id
		$sql = "DELETE FROM tblpages WHERE id IN ($id)";
		mysql_query($sql);
	}
	header('Location: ' . $_SESSION['shop_return_url']);

}

/*
 Remove a Page
 */
function deletePage()
{

	if (isset($_GET['id']) && (int)$_GET['id'] > 0) {
		$id = (int)$_GET['id'];
	} else {
		header('Location: index.php');
	}
	$sql = "DELETE FROM tblpages
	        WHERE id = $id";
	mysql_query($sql);

	header('Location: ' . $_SESSION['shop_return_url']);
}

?>